package com.grj.service.auth.controller;


import cn.hutool.jwt.JWTUtil;
import com.grj.api.user.UserApi;
import com.grj.model.user.convert.UserConvert;
import com.grj.model.user.dto.UserDTO;
import com.grj.model.user.entity.User;
import com.grj.model.user.vo.UserVO;
import common.filter.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 14505
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private UserApi userApi;

//    @GetMapping("/login")
//    public List<UserVO> login() {
//        return userApi.test().stream().map(i -> UserConvert.INSTANCE.dto2vo(i)).collect(Collectors.toList());
//    }

    @PostMapping("/login")
    public Result<Object> login(@RequestBody User user) {
        UserDTO info = userApi.info(user);
        Map<String, Object> map = new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("uid", info.getIddto());
                put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
            }
        };
        map.put("token", "Bearer " + JWTUtil.createToken(map, "1234".getBytes()));
        map.put("info", info);
        return Result.success(map);
    }
}
